package com.andooco.dataq.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.andooco.dataq.service.func.FuncService;
import com.andooco.dataq.service.func.dto.FuncDto;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;
import java.util.Map;

/**
 * 功能块操作管理
 *
 * @author andooco
 * 20201126
 */
@Slf4j
@Controller
@RequestMapping("/func")
public class FuncController {
    @Autowired
    private FuncService funcService;

    /**
     * 保存功能块信息 andooco 20201126
     *
     * @param form
     * @return
     * @throws Exception
     */
    @RequestMapping("/saveOrUpdate")
    @ResponseBody
    public String saveOrUpdate(Model model, FuncDto form) throws Exception {
        log.info("进入方法FuncController.save() {}", JSON.toJSONString(form));
        String result = funcService.saveOrUpdateFuncList(form);
        log.info("保存功能块结束：{}", result);
        return result;
    }

    /**
     * 功能块存量数据展示
     * andooco 20201125
     *
     * @param params
     * @return
     * @throws Exception
     */
    @RequestMapping("/list")
    @ResponseBody
    public JSONObject list(@RequestParam Map<String, Object> params) throws Exception {
        //查询功能块别名
        String funcName = (String) params.get("funcName");
        log.error("查询条件功能块别名：{}", funcName);
        List<FuncDto> temps = funcService.findFuncList(funcName);
        log.info("查询结果为：{}", JSON.toJSONString(temps));
        JSONObject result = new JSONObject();
        result.put("funcName", funcName);
        result.put("data", JSON.parseArray(JSON.toJSONString(temps)));
        return result;
    }

    /**
     * 删除功能块信息 andooco 20201127
     *
     * @param funcUid
     * @return
     * @throws Exception
     */
    @RequestMapping("/del")
    @ResponseBody
    public String del(Model model, String funcUid) throws Exception {
        log.info("进入方法FuncController.del() {}", funcUid);
        String result = funcService.delFuncList(funcUid);
        log.info("保存功能块结束：{}", result);
        return result;
    }
}
